# Packaging.cmake - 通用打包配置
function(configure_packaging)
    set(options)
    set(oneValueArgs PROJECT_NAME VERSION INSTALL_PREFIX ICON_PATH DESCRIPTION VENDOR DESKTOP_FILE)
    set(multiValueArgs TARGETS DEPENDS CATEGORIES MIME_TYPES)
    
    cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
    
    # 设置默认值
    if(NOT ARG_DESCRIPTION)
        set(ARG_DESCRIPTION "${ARG_PROJECT_NAME} Application")
    endif()
    if(NOT ARG_VENDOR)
        set(ARG_VENDOR "Unknown")
    endif()
    if(NOT ARG_CATEGORIES)
        set(ARG_CATEGORIES "Utility")
    endif()
    if(NOT ARG_INSTALL_PREFIX)
        set(ARG_INSTALL_PREFIX "/opt/apps/com.${ARG_PROJECT_NAME}")
    endif()
    if(NOT ARG_DESKTOP_FILE)
        set(ARG_DESKTOP_FILE "${ARG_PROJECT_NAME}.desktop")
    endif()
    
    message(STATUS "Packaging for Deepin/UOS system")
    message(STATUS "Project: ${ARG_PROJECT_NAME} ${ARG_VERSION}")
    message(STATUS "Install prefix: ${ARG_INSTALL_PREFIX}")
    
    # 设置安装前缀
    set(CMAKE_INSTALL_PREFIX "${ARG_INSTALL_PREFIX}" CACHE PATH "Installation prefix" FORCE)
    
    # 设置 CPack 通用变量
    set(CPACK_PACKAGE_NAME "${ARG_PROJECT_NAME}")
    set(CPACK_PACKAGE_VERSION "${ARG_VERSION}")
    set(CPACK_PACKAGE_VENDOR "${ARG_VENDOR}")
    set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${ARG_DESCRIPTION}")
    set(CPACK_PACKAGE_CONTACT "packager@example.com")
    set(CPACK_STRIP_FILES TRUE)
    set(CPACK_PACKAGING_INSTALL_PREFIX "${ARG_INSTALL_PREFIX}")
    
    # Deepin 特定设置
    set(CPACK_GENERATOR "DEB")
    set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${ARG_VENDOR}")
    
    # 设置依赖
    if(ARG_DEPENDS)
        set(CPACK_DEBIAN_PACKAGE_DEPENDS "${ARG_DEPENDS}")
    else()
        set(CPACK_DEBIAN_PACKAGE_DEPENDS "libqt6core6, libqt6gui6, libqt6widgets6, libqt6sql6, libsqlite3-0")
    endif()
    
    set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://gitee.com/${ARG_PROJECT_NAME}")
    set(CPACK_DEBIAN_PACKAGE_SECTION "utils")
    
    # 设置包文件名
    set(CPACK_PACKAGE_FILE_NAME "${ARG_PROJECT_NAME}-${ARG_VERSION}-${CMAKE_SYSTEM_PROCESSOR}")
    
    # 安装主程序到指定目录
    foreach(target IN LISTS ARG_TARGETS)
        install(TARGETS ${target}
            RUNTIME DESTINATION bin
            BUNDLE DESTINATION bin
            LIBRARY DESTINATION lib
            ARCHIVE DESTINATION lib
        )
    endforeach()
    
    # 生成桌面文件 - 使用绝对路径
    set(DESKTOP_CONTENT 
        "[Desktop Entry]
Version=1.0
Type=Application
Name=${ARG_PROJECT_NAME}
Name[zh_CN]=${ARG_PROJECT_NAME}
Comment=${ARG_DESCRIPTION}
Exec=${ARG_INSTALL_PREFIX}/bin/${ARG_PROJECT_NAME}
Icon=${ARG_PROJECT_NAME}
Categories=${ARG_CATEGORIES}
Terminal=false
StartupNotify=true
MimeType=${ARG_MIME_TYPES}
"
    )
    
    # 写入桌面文件
    set(DESKTOP_FILE_PATH "${CMAKE_CURRENT_BINARY_DIR}/${ARG_DESKTOP_FILE}")
    file(WRITE "${DESKTOP_FILE_PATH}" "${DESKTOP_CONTENT}")
    
    # 安装桌面文件到标准位置 - 这是关键修复！
    install(FILES "${DESKTOP_FILE_PATH}"
        DESTINATION /usr/share/applications
        RENAME "${ARG_PROJECT_NAME}.desktop"
    )
    
    # 安装图标文件
    if(ARG_ICON_PATH AND EXISTS "${ARG_ICON_PATH}")
        # 安装到应用目录
        install(FILES "${ARG_ICON_PATH}"
            DESTINATION ${ARG_INSTALL_PREFIX}/icons
            RENAME "${ARG_PROJECT_NAME}.png"
        )
        
        # 安装到系统图标目录（Deepin 启动器需要这个）
        install(FILES "${ARG_ICON_PATH}"
            DESTINATION /usr/share/icons/hicolor/256x256/apps
            RENAME "${ARG_PROJECT_NAME}.png"
        )
        
        # 安装到其他标准图标尺寸
        install(FILES "${ARG_ICON_PATH}"
            DESTINATION /usr/share/icons/hicolor/48x48/apps
            RENAME "${ARG_PROJECT_NAME}.png"
        )
        install(FILES "${ARG_ICON_PATH}"
            DESTINATION /usr/share/icons/hicolor/128x128/apps
            RENAME "${ARG_PROJECT_NAME}.png"
        )
    endif()
    
    # 创建应用信息文件（Deepin 启动器需要）
    set(APPINFO_CONTENT 
        "[Desktop Entry]
Name=${ARG_PROJECT_NAME}
Comment=${ARG_DESCRIPTION}
Exec=${ARG_INSTALL_PREFIX}/bin/${ARG_PROJECT_NAME}
Icon=${ARG_PROJECT_NAME}
Categories=${ARG_CATEGORIES}
Terminal=false
Type=Application
"
    )
    
    set(APPINFO_FILE "${CMAKE_CURRENT_BINARY_DIR}/${ARG_PROJECT_NAME}.appinfo")
    file(WRITE "${APPINFO_FILE}" "${APPINFO_CONTENT}")
    
    install(FILES "${APPINFO_FILE}"
        DESTINATION /usr/share/applications
        RENAME "${ARG_PROJECT_NAME}.appinfo"
    )
    
    # 包含 CPack
    include(CPack)
    
    message(STATUS "=== Deepin Packaging Summary ===")
    message(STATUS "Desktop file: /usr/share/applications/${ARG_PROJECT_NAME}.desktop")
    message(STATUS "Icons: /usr/share/icons/hicolor/*/apps/${ARG_PROJECT_NAME}.png")
    message(STATUS "Executable: ${ARG_INSTALL_PREFIX}/bin/${ARG_PROJECT_NAME}")
    message(STATUS "=== Packaging configured successfully ===")
endfunction()

# 辅助函数：验证桌面文件
function(validate_desktop_file PROJECT_NAME)
    # 检查桌面文件是否存在
    if(EXISTS "/usr/share/applications/${PROJECT_NAME}.desktop")
        message(STATUS "Desktop file verified: /usr/share/applications/${PROJECT_NAME}.desktop")
        
        # 检查桌面文件内容
        file(READ "/usr/share/applications/${PROJECT_NAME}.desktop" DESKTOP_CONTENT)
        if(DESKTOP_CONTENT MATCHES "Exec=.*${PROJECT_NAME}")
            message(STATUS "Desktop file Exec path is correct")
        else()
            message(WARNING "Desktop file Exec path may be incorrect")
        endif()
    else()
        message(WARNING "Desktop file not found in standard location")
    endif()
endfunction()